package com.junit;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hcfly.utils.ESClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;

import java.io.IOException;

public class DeleteQueryDemo {

    private static final ObjectMapper mapper = new ObjectMapper();
    private static final String index = "fruit";
    private static final String type = "species";
    private static final RestHighLevelClient client = ESClient.getClient();

    @Test
    public void DeleteByQuery() throws IOException {
        /**
         * 根据条件删除doc
         */
        DeleteByQueryRequest request = new DeleteByQueryRequest(index);
        request.types(type);

        // 2，指定检索条件
        request.setQuery(QueryBuilders.rangeQuery("size").lte(1));
        // 3 执行删除
        BulkByScrollResponse bulkByScrollResponse = client.deleteByQuery(request, RequestOptions.DEFAULT);

        // 4,输出返回结果
        System.out.println(bulkByScrollResponse.toString());
    }
}
